package com.tianfang.mytranmqconsumer.listener;

import com.alibaba.fastjson.JSONObject;
import com.tianfang.mytranmqconsumer.common.params.MailParam;
import com.tianfang.mytranmqconsumer.service.MailService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class MqListener {

    @Autowired
    private MailService mailService;

    private static final Log log = LogFactory.getLog(MqListener.class);

    /**
     * 监听和读取消息
     * 监听多少个队列就可以写多少下面的方法，只需把队列的名字改变一下和处理相应的业务逻辑
     * @param message
     */
    @JmsListener(destination="${spring.activemq.queue.name}")
    public void readActiveQueue(String message) {
        try {
            final String ms = message;
            log.info("==>receive message:" + ms);
            MailParam mailParam = JSONObject.parseObject(ms, MailParam.class);// 转换成相应的对象
            if (mailParam == null) {
                return;
            }
            try {
                mailService.mailSend(mailParam);
            } catch (Exception e) {
                log.error("==>MailException:", e);
            }
        } catch (Exception e) {
            log.error("==>", e);
        }
    }
}
